package com.hq.fxh.No2_BBS.Adapter;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/8/22
 * @Email xlwenhao1858@sina.com
 */

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hq.fxh.R;
import com.hq.fxh.Receiver.ListCallBack;
import com.hq.fxh.Utils.GlideUtils;
import com.hq.fxh.Utils.Tool;

import java.util.ArrayList;

public class Ladapter_EditAddPicture extends BaseAdapter {

    ArrayList<String> data;
    Activity activity;
    ListCallBack listCallBack;
    int maxPicture = 3;

    public Ladapter_EditAddPicture(Activity activity, ListCallBack listCallBack) {
        this.listCallBack = listCallBack;
        this.activity = activity;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public void setData(ArrayList<String> data) {
        this.data = data;
    }

    @Override
    public int getCount() {
        if (data != null && data.size() >= maxPicture) {
            return maxPicture;
        }
        return data == null ? 1 : (data.size() + 1);
    }

    @Override
    public Object getItem(int position) {
        if (data == null) {
            return null;
        } else if (position == data.size()) {
            return null;
        } else {
            return data.size();
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder = null;
        if (view == null || !(view.getTag() instanceof ViewHolder)) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.edit_addpicture_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (data == null) {
            viewHolder.editItemImageviewAdd.setImageResource(R.mipmap.tu_zeng);
            viewHolder.editItemImageviewAdd.setScaleType(ImageView.ScaleType.FIT_XY);
            viewHolder.layoutItemEditorPicRemovePic.setVisibility(View.GONE);

        } else if (data.size() >= maxPicture) {
            Tool.setUrlToImage(activity, data.get(position), viewHolder.editItemImageviewAdd);
            viewHolder.layoutItemEditorPicRemovePic.setVisibility(View.VISIBLE);

        } else if (data.size() < maxPicture) {
            if (position < data.size()) {
                viewHolder.layoutItemEditorPicRemovePic.setVisibility(View.VISIBLE);
                Tool.setUrlToImage(activity, data.get(position), viewHolder.editItemImageviewAdd);

            } else if (position == data.size()) {
                viewHolder.layoutItemEditorPicRemovePic.setVisibility(View.GONE);

                GlideUtils.loadImageViewFromMipmap(activity, R.mipmap.tu_zeng, viewHolder.editItemImageviewAdd);

            }

            viewHolder.layoutItemEditorPicRemovePic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listCallBack.onCall(view, position, null);
                }
            });


        }


        return view;
    }

    static class ViewHolder {
        protected ImageView editItemImageviewAdd;
        protected LinearLayout layoutItemEditorPicRemovePic;

        ViewHolder(View rootView) {
            initView(rootView);
        }

        private void initView(View rootView) {
            editItemImageviewAdd = (ImageView) rootView.findViewById(R.id.edit_item_imageview_add);
            layoutItemEditorPicRemovePic = (LinearLayout) rootView.findViewById(R.id.layout_itemEditorPic_RemovePic);
        }
    }
}

